//
//  TTSHotRankViewController.m
//  TTSVideo
//
//  Created by chenHR on 16/2/29.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TTSHotRankViewController.h"
#import "TTSCustomNavigation.h"
#import "TTSPublicCollectionViewCell.h"
#import "TTSHotRankModel.h"
#import "TTSAppToolsAFN.h"

@interface TTSHotRankViewController ()<TTSCustomNavigationDelegate, UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate>
/**
 *  collectionView
 */
@property (nonatomic, strong) UICollectionView *bigCollection;

/**
 *  标题数组
 */
@property (nonatomic, strong) NSMutableArray *titleArray;

/**
 *  自定义标题栏
 */
@property (nonatomic, strong) TTSCustomNavigation *customNa;

/**
 *  周排行数组
 */
@property (nonatomic, strong) NSMutableArray *weekArray;

/**
 *  月排行数组
 */
@property (nonatomic, strong) NSMutableArray *monthArray;

/**
 *  总排行数组
 */
@property (nonatomic, strong) NSMutableArray *totalArray;


@end

@implementation TTSHotRankViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor orangeColor];
    
    self.weekArray = [NSMutableArray array];
    self.monthArray = [NSMutableArray array];
    self.totalArray = [NSMutableArray array];
    
    // 右侧眼睛按钮
    UIImage *imgEye = [UIImage imageNamed:@"iconfont-yanjing"];
    imgEye = [imgEye imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:imgEye style:UIBarButtonItemStyleDone target:self action:@selector(eyeBtnAction:)];
    
    // 左侧菜单按钮
    UIImage *img = [UIImage imageNamed:@"iconfont-menu"];
    img = [img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:img style:UIBarButtonItemStyleDone target:self action:@selector(menuBtnAction:)];

    // 标题
    UIImageView *titleImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"title_meitu_1_meitu_2"]];
    self.navigationItem.titleView = titleImgView;

    
    // 添加标题栏
    self.titleArray = [NSMutableArray arrayWithObjects:@"周排行", @"月排行", @"总排行", nil];
    // 设置标题栏的frame   (高度为28)
    self.customNa = [[TTSCustomNavigation alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 28) titleArray:_titleArray titleColor:[UIColor blackColor] titleSelectedColor:[UIColor blackColor] lineColor:[UIColor grayColor] sizeOfFont:15];
    _customNa.delegate = self;
    _customNa.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_customNa];
    
    // 月排行
    NSString *monthUrl = @"http://baobab.wandoujia.com/api/v1/ranklist?strategy=monthly";
    // 周排行
    NSString *weekUrl = @"http://baobab.wandoujia.com/api/v1/ranklist?strategy=weekly";
    // 总排行
    NSString *historicalUrl = @"http://baobab.wandoujia.com/api/v1/ranklist?strategy=historical";
    
    [self TTSGetDataWithUrl:monthUrl];
    [self TTSGetDataWithUrl:weekUrl];
    [self TTSGetDataWithUrl:historicalUrl];
    

    
    /******* collectionView *******/
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    self.bigCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0,_customNa.frame.size.height, WIDTH,HEIGHT - _customNa.frame.size.height - 64 - 36) collectionViewLayout:layout];
    layout.itemSize = CGSizeMake(WIDTH, HEIGHT - _customNa.frame.size.height - 64 - 36);
    self.bigCollection.tag = 102030;
    self.bigCollection.delegate = self;
    self.bigCollection.dataSource = self;
    self.bigCollection.pagingEnabled = YES;
    self.bigCollection.bounces = NO;
    self.bigCollection.showsHorizontalScrollIndicator = NO;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.bigCollection.backgroundColor = [UIColor whiteColor];
    [self.bigCollection registerClass:[TTSPublicCollectionViewCell class] forCellWithReuseIdentifier:@"collectionCell"];
    [self.view addSubview:self.bigCollection];

    
}
/**
 *  解析数据
 *
 *  @param Url URL地址
 */
- (void)TTSGetDataWithUrl:(NSString *)Url{
    
    [TTSAppToolsAFN getDataWithAFNurl:Url block:^(id result) {
        NSMutableArray *videoList = [result objectForKey:@"videoList"];
        for (NSMutableDictionary *dic in videoList) {
            TTSHotRankModel *hotModel = [[TTSHotRankModel alloc] init];
            [hotModel setValuesForKeysWithDictionary:dic];
            if ([Url containsString:@"weekly"]) {
                [_weekArray addObject:hotModel];
            }
            if ([Url containsString:@"monthly"]) {
                [_monthArray addObject:hotModel];
            }
            if ([Url containsString:@"historical"]) {
                [_totalArray addObject:hotModel];
            }
        }
        [_bigCollection reloadData];
    } errorBlock:^(NSError *error) {
        
        
    }];
    
    
}


#pragma CollectionView协议方法实现
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _titleArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    TTSPublicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
    if (0 == indexPath.item) {
        [cell.tableArray removeAllObjects];
        cell.tableArray = _weekArray;
        return cell;
    }
    if (1 == indexPath.item) {
        [cell.tableArray removeAllObjects];
        cell.tableArray = _monthArray;
        return cell;
    }
    [cell.tableArray removeAllObjects];
    cell.tableArray = _totalArray;
    return cell;
}

/**
 *  滑动collectionView选中的标题
 *
 *  @param scrollView collectionView
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView.tag == 102030) {
        NSInteger index = scrollView.contentOffset.x / WIDTH;
        [_customNa changeTitleByValue:index + 1];
    }
}

/**
 *  实现协议方法,当前点击的标题
 *
 *  @param titleNumber 点击的btn的tag
 */
- (void)getPresentTitleNumber:(NSInteger)titleNumber{
  _bigCollection.contentOffset = CGPointMake(self.view.frame.size.width * (titleNumber - 1), 0);
}

/**
 *  右上角眼睛按钮点击方法
 *
 *  @param btn 监听按钮
 */
- (void)eyeBtnAction:(UIBarButtonItem *)btn{
    NSLog(@"眼睛");
}

/**
 *  菜单按钮
 *
 *  @param btn 监听按钮
 */
- (void)menuBtnAction:(UIButton *)btn{
    NSLog(@"菜单");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
